<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>promise的方法</title>
</head>

<body>
  <script>
    // p.then(成功的函数，失败的函数)

    // .then(成功的函数，失败的)
    // .catch(失败的函数)
    // .finally(不管结果如何，都会执行)  没有参数

    let p = new Promise((resolve,reject) => {
      setTimeout(() => {
        let num = parseInt(Math.random() * 30)
        if (num <= 10) {
          resolve(num)
        }else{
          reject(num)
        }
      }, 1000);
    })

    p.then(
      function (res) {
        console.log(res)
      }
    ).catch(
      err =>{
        console.log(err)
      }
    ).finally(
      ()=>{
        console.log('不论结果如何,这段代码一定执行')
      }
    )
  </script>
</body>

</html>